'use strict';

const Controller = require('egg').Controller;
class LunboController extends Controller {
  async findAllLunbo() {
    const { ctx } = this;
    const data = await ctx.service.lunboService.selectAll();
    ctx.body = { code: 200, data };
  }
  async addLunbo() {
    const { ctx } = this;
    if (!ctx.request.body.imgTitle) {
      ctx.body = { code: 501, msg: '缺少必填参数,轮播图的标题不能为空!' };
    } else if (!ctx.request.body.imgUrl) {
      ctx.body = { code: 501, msg: '缺少必填参数,请上传图片!' };
    } else {
      const data = await ctx.service.lunboService.addImage(ctx.request.body.imgTitle, ctx.request.body.imgUrl);
      ctx.body = { data };
    }
  }
  async deleteLunbo() { // 根据_id删除轮播图
    const { ctx } = this;
    const { id } = ctx.params;
    if (!id) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入轮播图的_id字段!' };
    } else {
      const data = await ctx.service.lunboService.deleteImage(id);
      ctx.body = { code: 200, data };
    }
  }
  async deleteAllLunbo() { // 删除全部轮播图
    const { ctx } = this;
    const data = await ctx.service.lunboService.deleteAllLunbo();
    ctx.body = { code: 200, data };
  }
  async updateLunbo() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const { imgTitle } = ctx.request.body;
    const { imgUrl } = ctx.request.body;
    if (!imgTitle) {
      ctx.body = { code: 501, msg: '缺少必填参数,轮播图的标题不能为空!' };
    } else if (!imgUrl) {
      ctx.body = { code: 501, msg: '缺少必填参数,请上传图片!' };
    } else if (!id) {
      ctx.body = { code: 501, msg: '缺少必填参数,请输入_id' };
    } else {
      const data = await ctx.service.lunboService.updateLunbo(id, imgTitle, imgUrl);
      ctx.body = { data };
    }
  }
}

module.exports = LunboController;
